#!/command/with-contenv bashio
# vim: ft=bash
# shellcheck shell=bash
# ==============================================================================
# Start Whisper service
# ==============================================================================
flags=()

if [ "$(uname -m)" == "x86_64" ] && ! grep -qw "avx" /proc/cpuinfo; then
    bashio::log.warning "Your CPU does not support Advanced Vector Extensions (AVX). Whisper will run slower than normal."
fi

if [[ "${WHISPER_DEBUG:-}" == "true" ]]; then
    flags+=("--debug")
fi

if [[ "${WHISPER_OFFLINE:-}" == "true" ]]; then
    flags+=("--local-files-only")
fi

if [[ -n "${WHISPER_INITIAL_PROMPT:-}" ]]; then
    flags+=("--initial-prompt" "${WHISPER_INITIAL_PROMPT}")
fi

exec python3 -m wyoming_faster_whisper \
    --uri "tcp://0.0.0.0:${WHISPER_PORT}" \
    --model "${WHISPER_MODEL}" \
    --beam-size "${WHISPER_BEAM_SIZE}" \
    --compute-type "${WHISPER_COMPUTE_TYPE}" \
    --language "${WHISPER_LANGUAGE}" \
    --device cuda \
    --zeroconf nexus-stt \
    --data-dir /data/models/faster-whisper \
    --download-dir /data/models/faster-whisper \
    --stt-library faster-whisper \
    ${flags[@]}
